//
//  ReportViewController.m
//  quickBatchManager
//
//  Created by jack on 15/12/8.
//  Copyright © 2015年 jack. All rights reserved.
//

#import "ReportViewController.h"
#import "PMCalendar.h"
#import "DateUtils.h"
@interface ReportViewController ()<PMCalendarControllerDelegate>
{
    int timePos;
    NSDate *startDate;
    NSDate *endDate;
    
    int chuhuoNum;
    float chuhuoMoney;
    int tuihuoNum;
    float tuihuoMoney;
    float weifuMoney;
    float molinMoney;
    float zhifuMoney;
}
@property (nonatomic, strong) PMCalendarController *pmCC;

@end

@implementation ReportViewController
@synthesize pmCC;
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}




-(void)initUI
{
    startDate = [NSDate date];
    endDate = [NSDate date];
    _startTimeLabel.text = [DateUtils dateToStringByFormat:startDate format:YYYYMMDD];
    _endTimeLabel.text = [DateUtils dateToStringByFormat:endDate format:YYYYMMDD];
    _startTimeLabel.userInteractionEnabled = YES;
    _endTimeLabel.userInteractionEnabled = YES;
    _closeBtn.userInteractionEnabled = YES;
    [_closeBtn bk_whenTapped:^{
        [self.navigationController popViewControllerAnimated:YES];
    }];
    
    [_startTimeLabel bk_whenTapped:^{
        timePos = 0;
        self.pmCC = [[PMCalendarController alloc] initWithThemeName:@"default"];
        self.pmCC.delegate = self;
        self.pmCC.mondayFirstDayOfWeek = NO;
        if ([self.pmCC isCalendarVisible])
        {
            [self.pmCC dismissCalendarAnimated:NO];
        }
        
        [self.pmCC presentCalendarFromView:_startTimeLabel
                  permittedArrowDirections:PMCalendarArrowDirectionDown
                                 isPopover:YES
                                  animated:YES];
        
        self.pmCC.period = [PMPeriod oneDayPeriodWithDate:startDate];

    }];
    
    
    [_endTimeLabel bk_whenTapped:^{
        timePos = 1;
        self.pmCC = [[PMCalendarController alloc] initWithThemeName:@"default"];
        self.pmCC.delegate = self;
        self.pmCC.mondayFirstDayOfWeek = NO;
        if ([self.pmCC isCalendarVisible])
        {
            [self.pmCC dismissCalendarAnimated:NO];
        }
       
        
        [self.pmCC presentCalendarFromView:_endTimeLabel
                  permittedArrowDirections:PMCalendarArrowDirectionDown
                                 isPopover:YES
                                  animated:YES];
         self.pmCC.period = [PMPeriod oneDayPeriodWithDate:endDate];
        
    }];
    
    [_wechatBtn bk_whenTapped:^{
        [UMSocialData defaultData].extConfig.wxMessageType = UMSocialWXMessageTypeImage;
        [UMSocialSnsService presentSnsIconSheetView:self
                                             appKey:UM_KEY
                                          shareText:nil
                                         shareImage:[ToolUtils snapshot:self.view]
                                    shareToSnsNames:@[UMShareToWechatSession,UMShareToWechatTimeline]
                                           delegate:nil];
    }];
    
    
    [self setData];
    
}






-(void)setData
{
    chuhuoNum= 0;
    chuhuoMoney = 0;
    tuihuoMoney = 0;
    tuihuoNum =  0;
    weifuMoney = 0;
    molinMoney = 0;
    zhifuMoney = 0;
    NSMutableArray *saleOrderArray = [SaleOrderModel searchWithWhere:[NSString stringWithFormat:@"date >= '%@' and date <= '%@'",_startTimeLabel.text,_endTimeLabel.text]];
    for (SaleOrderModel *model in saleOrderArray) {
        chuhuoNum = chuhuoNum+model.totalNumber;
        chuhuoMoney = chuhuoMoney +model.totalPrice * +model.totalNumber;
        
        if(!model.isPay)
        {
            weifuMoney = weifuMoney + model.unpaidPrice;
        }else
        {
            zhifuMoney = zhifuMoney + model.paidPrice;
        }
        
        molinMoney = molinMoney+model.moLingPrice;
        
        
        NSMutableArray *orderInfoArray = [SaleOrderInfoModel searchWithWhere:[NSString stringWithFormat:@"saleOrderId=%ld",model.saleOrderId]];
        for (SaleOrderInfoModel *infoModel in orderInfoArray) {
            if(infoModel.productNumber<0)
            {
                tuihuoNum = tuihuoNum+labs(infoModel.productNumber);
                tuihuoMoney = tuihuoNum+ tuihuoNum *infoModel.price;
            }
        }
    }
    
    _chuhuoNumLabel.text = [NSString stringWithFormat:@"%d件",chuhuoNum];
    _chuhuoMoneyLabel.text = [NSString stringWithFormat:@"%0.2f元",chuhuoMoney];
    
    _tuihuoNumLabel.text = [NSString stringWithFormat:@"%d件",tuihuoNum];
    _tuihuoMoneyLabel.text = [NSString stringWithFormat:@"%0.2f元",tuihuoMoney];
    
    _shixiaoNumLabel.text = [NSString stringWithFormat:@"%d件",chuhuoNum-tuihuoNum];
    _shixiaoMoneyLabel.text = [NSString stringWithFormat:@"%0.2f元",chuhuoMoney-tuihuoMoney];
    
    
    _weifuMoneyLabel.text = [NSString stringWithFormat:@"%0.2f元",weifuMoney];
    _molinMoneyLabel.text = [NSString stringWithFormat:@"%0.2f元",molinMoney];
    _zhichuMoneyLabel.text = [NSString stringWithFormat:@"%0.2f元",zhifuMoney];
    
    
    
    double money1 = [PayInModel sumWithWhere:@"amountPayType1" where:[NSString stringWithFormat:@"payDate >= '%@' and payDate <= '%@'",_startTimeLabel.text,_endTimeLabel.text]];
    double money2 = [PayInModel sumWithWhere:@"amountPayType2" where:[NSString stringWithFormat:@"payDate >= '%@' and payDate <= '%@'",_startTimeLabel.text,_endTimeLabel.text]];
    double money3 = [PayInModel sumWithWhere:@"amountPayType3" where:[NSString stringWithFormat:@"payDate >= '%@' and payDate <= '%@'",_startTimeLabel.text,_endTimeLabel.text]];
    double money4 = [PayInModel sumWithWhere:@"amountPayType4" where:[NSString stringWithFormat:@"payDate >= '%@' and payDate <= '%@'",_startTimeLabel.text,_endTimeLabel.text]];
    double money5 = [PayInModel sumWithWhere:@"amountPayType5" where:[NSString stringWithFormat:@"payDate >= '%@' and payDate <= '%@'",_startTimeLabel.text,_endTimeLabel.text]];

    
    
    NSArray *datas = [[NSString stringWithFormat:@"%@%@",[CoreArchive strForKey:NSDEFAULT_ST_PAYMENT],[CoreArchive boolForKey:NSDEFAULT_ST_PAYMENT_DS ]?@"|代收":@""] componentsSeparatedByString:@"|"];
    

 
    
    NSArray *views = [_contentView subviews];
    for(UIView* view in views)
    {
        [view removeFromSuperview];
    }
    
    
    for (int i=0; i<datas.count; i++) {
        UIView *typeView  = [[UIView alloc] initWithFrame:CGRectMake(10, 10*i, _contentView.frame.size.width-20, 30)];
        UILabel *typeNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, typeView.frame.origin.y, 80, 30)];
        typeNameLabel.text = [datas objectAtIndex:i];
        double money = [SaleOrderModel sumWithWhere:@"orderPrice" where:[NSString stringWithFormat:@"tspayType='%@' and date>='%@' and date<='%@'",[datas objectAtIndex:i],_startTimeLabel.text,_endTimeLabel.text]];
        

        UILabel *money1Lael = [[UILabel alloc] initWithFrame:CGRectMake(120, typeView.frame.origin.y, 80, 30)];
        money1Lael.text = [NSString stringWithFormat:@"%0.2f元",money];
        UILabel *money2Lael = [[UILabel alloc] initWithFrame:CGRectMake(240, typeView.frame.origin.y, 80, 30)];

        UILabel *money3Lael = [[UILabel alloc] initWithFrame:CGRectMake(360, typeView.frame.origin.y, 80, 30)];
       // money3Lael.text = @"0元";
        [typeView addSubview:typeNameLabel];
     
        
        
        if(i==0)
        {
             money2Lael.text = [NSString stringWithFormat:@"%0.2f元",money1];
             money3Lael.text = [NSString stringWithFormat:@"%0.2f",money +money1];
        }
        if(i==1)
        {
            money2Lael.text = [NSString stringWithFormat:@"%0.2f元",money2];
             money3Lael.text = [NSString stringWithFormat:@"%0.2f",money +money2];
        }
        if(i==2)
        {
             money2Lael.text = [NSString stringWithFormat:@"%0.2f元",money3];
             money3Lael.text = [NSString stringWithFormat:@"%0.2f",money +money3];
        }
        
        if(i==3)
        {
             money2Lael.text = [NSString stringWithFormat:@"%0.2f元",money4];
             money3Lael.text = [NSString stringWithFormat:@"%0.2f",money +money4];
        }
           
        if(i==4)
        {
            money2Lael.text = [NSString stringWithFormat:@"%0.2f元",money5];
             money3Lael.text = [NSString stringWithFormat:@"%0.2f",money +money5];
        }
        
        
        [typeView addSubview:money1Lael];
        [typeView addSubview:money2Lael];
        [typeView addSubview:money3Lael];
        [_contentView addSubview:typeView];
        
        
    }
    
    NSMutableArray *batchArray = [BatchProductModel searchWithWhere:[NSString stringWithFormat:@"createDate >= '%@' and createDate <= '%@'",_startTimeLabel.text,_endTimeLabel.text]];
    double daohuoCount = [BatchProductModel sumWithWhere:@"totalNumber" where:[NSString stringWithFormat:@"createDate >= '%@' and createDate <= '%@'",_startTimeLabel.text,_endTimeLabel.text]];
    
    NSMutableArray *productArray  = [ProductModel searchWithWhere:[NSString stringWithFormat:@"isDel = 1 and createDate >= '%@' and createDate <= '%@'",_startTimeLabel.text,_endTimeLabel.text]];
    
    if(batchArray && [batchArray count])
    {
        _piciNumLabel.text = [NSString stringWithFormat:@"%lu批",(unsigned long)[batchArray count]];
        _daohuoCountNumLabel.text = [NSString stringWithFormat:@"%0.0f件",daohuoCount];
    }
    
    if(productArray && [productArray count])
    {
        _delNumLabel.text = [NSString stringWithFormat:@"%lu件",(unsigned long)[productArray count]];
    }
    
    _countLabel.text =  [NSString stringWithFormat:@"%0.0f件",[productArray count] + daohuoCount];
    
}




#pragma mark PMCalendarControllerDelegate methods

- (void)calendarController:(PMCalendarController *)calendarController didChangePeriod:(PMPeriod *)newPeriod
{
    
    //    nowDate=newPeriod.startDate;
    //    dateLabel.text = [NSString stringWithFormat:@"%@"
    //                      , [newPeriod.startDate dateStringWithFormat:@"yyyy-MM-dd"]];
    
    if(timePos==0)
    {
        // NSString *nowDate=newPeriod.startDate;
        _startTimeLabel.text = [NSString stringWithFormat:@"%@"
                                , [newPeriod.startDate dateStringWithFormat:@"yyyy-MM-dd"]];
        startDate = newPeriod.startDate;
        
    }else
    {
        _endTimeLabel.text = [NSString stringWithFormat:@"%@"
                              , [newPeriod.startDate dateStringWithFormat:@"yyyy-MM-dd"]];
        endDate = newPeriod.endDate;
    }
    
    [self setData];
}

@end
